package org.bistu.course.controller;

import java.util.List;

import org.bistu.course.bean.Grade;
import org.bistu.course.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class GradeController {
	@Autowired
	private GradeService gradeService;
	
	@RequestMapping("/listGrade")
	public String listGrade(Model model) {
		List<Grade> grades = gradeService.findAll();
		model.addAttribute("grades", grades);
		return "listGrade";
	}
	
	@RequestMapping("/toAddGrade")
	public String toAdd() {
		return "/addGrade";
	}
	
	@RequestMapping("/addGrade")
	public String addgrade(Grade grade) {
		gradeService.addGrade(grade);
		return "redirect:/listGrade";
	}
	
	@RequestMapping("/toUpdateGrade")
	public String toUpdata(Model model,int id) {
		Grade grade = gradeService.findById(id);
		model.addAttribute("grade", grade);
		return "/updateGrade";
	}
	
	@RequestMapping("/updateGrade")
	public String updategrade(Grade grade) {
		gradeService.updateGrade(grade);
		return "redirect:/listGrade";
	}
	
	@RequestMapping("/deleteGrade")
	public String deletegrade(int id) {
		gradeService.deleteGrade(id);
		return "redirect:/listGrade";
	}
}
